const app = getApp()

Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    theme: app.globalData.theme, //全局颜色变量
    config: app.globalData.config,
    year: 0,
    month: 0,
    today: 0,
    space: 0,
    day: [],
    orderCountMap: {},
    todayObj: {
      year: 0,
      month: 0,
      today: 0
    },
    // 施工中
    num2: 0,
    // 已完成
    num3: 0
  },
  onShow() {
    app.initPage()
        .then(res => {
          this.getNowTime();
          this.getSpace();
          this.createDay();
          this.loadOrderNum();
        })
  },
  onLoad: function () {
  },
  loadOrderNum() {
    let orderCountMap = {}
    let num2 = 0
    let num3 = 0
    let date = this.data.year + "-" + (this.data.month < 10 ? ('0' + this.data.month) : this.data.month) + "-01"
    app.api.providerOrderCountAllWithMonth({
      date: date
    }).then(res => {
      let orderCountList = res.data || []
      orderCountList.forEach(function (item, index) {
        orderCountMap[item.day] = item
        num2 += item.num2
        num3 += item.num3
      })
      this.setData({
        orderCountMap: orderCountMap,
        num2: num2,
        num3: num3
      })
    })
  },
  getNowTime() {
    const time = new Date();
    this.setData({
      year: time.getFullYear(),
      month: time.getMonth() + 1,
      today: time.getDate(),
      week: time.getDay(),
      todayObj: {
        year: time.getFullYear(),
        month: time.getMonth() + 1,
        today: time.getDate(),
      }
    })
  },
  createDay() {
    let day = [];
    let time = new Date();
    time.setMonth(this.data.month);
    time.setDate(0);
    let len = time.getDate();
    for (let i = 1; i <= len; i++) {
      day.push(i);
    }
    this.setData({
      day
    })
  },
  getSpace() {
    let time = new Date(this.data.year, this.data.month - 1, 1);
    time = new Date(time.setDate(0))
    const space = time.getDay();
    this.setData({
      space
    })
  },
  // 切换下个月
  next() {
    let month = this.data.month;
    if (month == 12) {
      this.setData({
        year: this.data.year + 1,
        month: 1
      })
    } else {
      this.setData({
        month: month + 1
      })
    }
    this.loadOrderNum()
    this.getSpace();
    this.createDay();
    this.check();
    // this.emit();
  },
  // 今月选择31，下月只有30，那么选择的today更改为30
  check() {
    let {
      today,
      year,
      month
    } = this.data;
    const time = new Date(year, month, 0);
    let maxDay = time.getDate()
    if (today > maxDay) {
      this.setData({
        today: maxDay
      })
    }
  },
  // 切换上个月
  last() {
    let month = this.data.month;
    if (month == 1) {
      this.setData({
        year: this.data.year - 1,
        month: 12
      })
    } else {
      this.setData({
        month: month - 1
      })
    }
    this.loadOrderNum()
    this.getSpace();
    this.createDay();
    this.check();
    // this.emit();
  },
  // 日历点击事件
  click(e) {
    // 显示
    let today = e.currentTarget.dataset.today;
    if (this.data.orderCountMap[today]) {
      let date = this.data.year + '-' + (this.data.month < 10 ? ('0' + this.data.month) : this.data.month) + '-' + (today < 10 ? ('0' + today) : today)
      wx.navigateTo({
        url: "/pages/order/order-list/index?date=" + date
      })
    }
    // this.setData({
    //     today
    // })
    // this.emit();
  },
  emit() {
    // 传递给父组件
    let {
      year,
      month,
      today
    } = this.data;
    let time = new Date(year, month - 1, today);
    let week = time.getDay();
    week = this.formatWeek(week)
    this.triggerEvent('click', {
      year,
      month,
      today,
      week
    }, {})
  },
  formatWeek(data) {
    switch (data) {
      case 1:
        return '一';
      case 2:
        return '二';
      case 3:
        return '三';
      case 4:
        return '四';
      case 5:
        return '五';
      case 6:
        return '六';
      case 0:
        return '七';
    }
  }
})